#!/bin/sh
# based on the Slackware 12.0 SlackBuild
# Packager Rob Cotrone  ( WeeboTech at mydomain dot com)
# http://lime-technology.com
# Required: 

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
NAME=syslogmon+webGui
PKG=$TMP/package-${NAME}
VERSION=1.0.1
ARCH=${ARCH:-unRAID}
BUILD=WeeboTech
SOURCE=http://unraid-webgui.googlecode.com/files/${NAME}-${VERSION}.tar.gz

if [ "$1" = "--clean" ]
   then rm -vf ${NAME}-${VERSION}.tar.gz $NAME-$VERSION-$ARCH-$BUILD.tgz
	rm -vrf ${PKG}
	exit
fi

if [ "$1" = "--makedist" -o "$1" = "--dist" ]
   then echo "Building SOURCE Distribution: ${NAME}-${VERSION}.tar.gz"	
	echo
 	rm -vf "$NAME-$VERSION-$ARCH-$BUILD.tgz" "${NAME}-${VERSION}.tar.gz"
        tar -C .. -cvzf ${NAME}-${VERSION}.tar.gz ${NAME}-${VERSION}
	echo
	ls -l ${NAME}-${VERSION}.tar.gz
	echo
fi

if [ ! -e $NAME-$VERSION.tar.gz ]
   then echo wget -c $SOURCE
fi

if [ ! -d $TMP ]
   then mkdir -p $TMP
fi

if [ ! -d $PKG ]
   then mkdir -p $PKG
   else rm -rf ${PKG}
        mkdir ${PKG}
fi

cd $TMP

if [ -f $CWD/$NAME-$VERSION.tar.gz ]
   then echo "Using SOURCE Distribuition: $CWD/$NAME-$VERSION.tar.gz"
	tar xvzf $CWD/$NAME-$VERSION.tar.gz
   else echo -e "\n\E[1;33m"
        echo -e "missing SOURCE Distribution: $CWD/$NAME-$VERSION.tar.gz"
        echo -e "\E[0;0m\n"
	echo "Assuming files are in ${CWD}"
        sleep 0
fi

echo -e "\n\E[0;32mStart SlackBuild $NAME-$VERSION\E[0;0m\n"

if [ ! -d $NAME-$VERSION ] 
   then mkdir -p $NAME-$VERSION
fi

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

# chown -R root:root .

# Here is where the manual build install commands go.

WEBGUIDIR="usr/local/webGui"
install -d ${PKG}/${WEBGUIDIR}
FRC=0
while read FILE
do  [ -z "${FILE%\#*}" ] && continue
    if [ ! -f "${FILE}" ] 
       then echo -e "\n\E[1;31mMISSING: ${FILE}\E[0;0m\n"
            FRC=1
    fi
    F=${FILE##*/}           # basename of file
    D=${FILE%%$F}           # dirname of file
    B=${F%.*}               # strip off after last . character
    E=${F#${B}\.}           # get extension after last .
    # echo "D=${D}, F=${F}, B=${B}, E=${E}"
    install -p -m640 ${FILE} ${PKG}/${WEBGUIDIR}
    if [ "${E}" = "page" ]
       then sed -i "s/Version=.*/Version=\"${VERSION}\"/g" ${PKG}/${WEBGUIDIR}/${F}
    fi
    if [ "${E}" = "php" ]; then 
       php -l ${FILE}
       if [ $? -gt 0 ]; then 
       	  echo -e "\n\E[1;31mPHP Syntax Check Failed on: ${FILE}\E[0;0m\n"
	  # sleep 3
	  exit
       fi
    fi
done <<-EOF
# List your files here. You can comment them out if needed.
${CWD}/syslog.page
${CWD}/syslogmon.page
${CWD}/syslogmon.php
${CWD}/syslogmon.png
EOF

if [ $FRC -gt 0 ]
   then echo "Aborted."; exit
fi

install -m755 -d ${PKG}/etc/syslogmon
install -m644 ${CWD}/exclude ${PKG}/etc/syslogmon/exclude.new

# The rest should be standard on all installations

mkdir -p ${PKG}/usr/doc/${NAME}-${VERSION}
sed "s/#.##/${VERSION}/g" < ${CWD}/slack-desc > ${PKG}/usr/doc/${NAME}-${VERSION}/slack-desc
cat ${CWD}/${NAME}.SlackBuild > ${PKG}/usr/doc/${NAME}-${VERSION}/${NAME}.SlackBuild

mkdir -p ${PKG}/install
sed "s/#.##/${VERSION}/g" < ${CWD}/slack-desc > ${PKG}/install/slack-desc
if [ -f ${CWD}/doinst.sh ]
   then cat $CWD/doinst.sh  > $PKG/install/doinst.sh
fi

if [ -d ${PKG}/usr/man ]
   then find "${PKG}/usr/man" -type f -exec gzip {} \;
fi

cd ${PKG}
# requiredbuilder -y -v -s ${CWD} ${PKG}
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]
   then rm -rf $TMP
	shift
fi

if [ -f ${CWD}/${NAME}-${VERSION}.tar.gz ]
   then ls -l ${CWD}/${NAME}-${VERSION}.tar.gz
fi

if [ -f ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz ]
   then ls -l ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz
fi

if [ "$1" = "--install" ]
   then echo
	installpkg ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz
	shift
fi

if [ "$1" = "--deploy" ]
   then echo
	cp -v ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz /boot/extra
        shift
fi

if [ "$1" = "--dist" ]
   then echo
	cp -v ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz /boot/Packages/Uploads
        cp -v ${CWD}/${NAME}-${VERSION}.tar.gz /boot/Packages/Uploads
        shift
fi
